package holdtime.xlxc.base;

import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;

import holdtime.xlxc.R;
import holdtime.xlxc.model.QueryAddress;

public class BaseActivity extends AppCompatActivity {

    public static final int LOCATION_CODE = 1;//请求定位
    public static final int REQUEST_ENABLE_BT = 2;//请求打开蓝牙
    public static final int SETTING_CODE = 3;//设置页面
    public static final int MODIFY_NAME_CODE = 4;//修改昵称
    public static final int MODIFY_CARD_CODE = 5;//修改身份证号
    public static final int PERSONAL_INFO_CODE = 6;//修改个人信息
    public static final int IMAGE_REQUEST_CODE = 7;//从相册获取
    public static final int CAMERA_REQUEST_CODE = 8;//从相机获取
    public static final int RESULT_IMAGE_CUT_CODE = 9;//剪裁后的图片
    public static final int CAMERA_CODE = 10;//请求调用相机
    public static final int CALL_CODE = 11;//请求拨号
    public static final int LOGIN_CODE = 12;//登录后跳转

    public String TAG = "XLXC";
    public String actionBarTitle;//导航栏标题
    public boolean isSetActionBar = true;
    public boolean homeAsUpEnabled = false;//是否显示导航栏返回按钮
    public Drawable indicator;
    public MaterialDialog dialog;
    public QueryAddress addressManager;

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG, "onResume()");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e(TAG, "onPause()");
        dialog.dismiss();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "onDestroy()");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
        Log.e(TAG, "onCreate()");

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//禁止横屏
        setActionBar();//设置导航栏
        setDialog();//设置提示框
        addressManager = new QueryAddress();
    }

    private void setActionBar() {
        ActionBar actionBar = getSupportActionBar();
        assert actionBar != null;
        if (isSetActionBar) {
            actionBar.setBackgroundDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.nav_white, null));
            ActionBar.LayoutParams params = new ActionBar.LayoutParams(
                    ActionBar.LayoutParams.WRAP_CONTENT,
                    ActionBar.LayoutParams.WRAP_CONTENT,
                    Gravity.CENTER);
            View customView = getLayoutInflater().inflate(R.layout.actionbar_layout, new LinearLayout(this), false);
            TextView titleTV = (TextView) customView.findViewById(R.id.actionbar_textview);
            titleTV.setText(actionBarTitle);

            actionBar.setCustomView(customView, params);
            actionBar.setDisplayShowCustomEnabled(true);
            actionBar.setDisplayShowTitleEnabled(false);
            actionBar.setDisplayHomeAsUpEnabled(homeAsUpEnabled);
            actionBar.setHomeAsUpIndicator(indicator);
            actionBar.setElevation(0);//android5.0以上移除底部阴影
        } else {
            actionBar.hide();
        }
    }

    private void setDialog() {
        dialog = new MaterialDialog.Builder(this)
                .title("请稍后")
                .content("加载中")
                .progress(true, 0)
                .progressIndeterminateStyle(false)
                .widgetColor(Color.parseColor("#FF7814"))
                .build();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}
